<template>
  <view>
    <!-- 
      吸顶: 正常处于标准流, 一旦这个盒子要看不见了 就处于固定定位 
       粘性 sticky
     -->
    <!-- 1. @click.native="goSearch" -->
    <!-- 2. 组件内部触发这个自定义事件即可 -->
    <view>这是一个广告</view>
    <view>这是一个广告</view>
    <view>这是一个广告</view>
    <view>这是一个广告</view>

    <view class="sticky-box">
      <MySearch @click="goSearch"></MySearch>
    </view>

    <!-- 轮播图区域 -->
    <swiper
      class="top-swiper"
      :indicator-dots="true"
      :autoplay="true"
      :interval="3000"
      :duration="1000"
      :circular="true"
    >
      <!-- 循环渲染轮播图的 item 项 -->
      <swiper-item v-for="(item, index) in swiperList" :key="index">
        <navigator
          class="swiper-content"
          :url="`/pages_sub1/product-detail/product-detail?pid=${item.goods_id}`"
        >
          <image :src="item.image_src"></image>
        </navigator>
      </swiper-item>
    </swiper>

    <!-- 分类导航区域 -->
    <view class="nav-list">
      <view
        @click="navClickHandler(item)"
        class="nav-item"
        v-for="(item, index) in navList"
        :key="index"
      >
        <image :src="item.image_src" class="nav-img"></image>
      </view>
    </view>

    <!-- 楼层区域 -->
    <view class="floor-list">
      <!-- 楼层 item 项 -->
      <view class="floor-item" v-for="(item, index) in floorList" :key="index">
        <!-- 楼层标题 -->
        <image :src="item.floor_title.image_src" class="floor-title"></image>

        <!-- 楼层图片区域 -->
        <view class="floor-img-box">
          <!-- 左侧 1 个大图 -->
          <view class="left-img-box">
            <image
              :src="item.product_list[0].image_src"
              :style="{ width: item.product_list[0].image_width + 'rpx' }"
              mode="widthFix"
              @click="jumpList(item.product_list[0])"
            ></image>
          </view>

          <!-- 右侧 4 个小图 -->
          <view class="right-img-box">
            <block v-for="(product, idx) in item.product_list" :key="idx">
              <view class="right-img-item" v-if="idx !== 0">
                <image
                  :src="product.image_src"
                  :style="{ width: product.image_width + 'rpx' }"
                  mode="widthFix"
                  @click="jumpList(product)"
                ></image>
              </view>
            </block>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
// import http from '@/utils/request'
import MySearch from '@/components/my-search.vue'
export default {
  components: {
    MySearch,
  },
  data() {
    return {
      swiperList: [],
      navList: [],
      floorList: [],
    }
  },
  created() {
    // this.$http.get('/home/swiperdata')

    this.getSwiperList()
    this.getNavList()
    this.getFloorList()
  },
  methods: {
    async getSwiperList() {
      const { message, meta } = await this.$http.get('/home/swiperdata')
      // console.log(res);

      if (meta.status !== 200) return this.$msg()

      // this.$msg('哈哈哈', 3000, 'success')
      this.swiperList = message
    },
    async getNavList() {
      const { meta, message } = await this.$http.get('/home/catitems')

      // 请求失败处理
      if (meta.status !== 200) {
        return this.$msg()
      }

      this.navList = message
    },
    navClickHandler(item) {
      // console.log(item)
      if (item.name === '分类') {
        // console.log('111')
        // 跳转到分类页(tabbar)
        // wx.navigateTo wx.redirectTo wx.switchTab
        // uni.navigateTo uni.redirectTo uni.switchTab
        uni.switchTab({
          url: '/pages/category/category',
        })
      }
    },
    async getFloorList() {
      const { meta, message } = await this.$http.get('/home/floordata')

      // 请求失败处理
      if (meta.status !== 200) {
        return this.$msg()
      }

      this.floorList = message
    },
    jumpList(product) {
      // console.log(product)

      const tempStr = product.navigator_url.split('?')[1]
      uni.navigateTo({
        url: '/pages_sub1/product-list/product-list?' + tempStr,
      })
    },
    goSearch() {
      // console.log('去搜索页吧')
      uni.navigateTo({
        url: '/pages_sub1/search/search',
      })
    },
  },
}
</script>

<style lang="scss">
.sticky-box {
  position: sticky; // 粘性定位

  top: 0; // 开启粘性定位的条件

  z-index: 999;
}

.top-swiper {
  height: 330rpx;

  .swiper-content,
  image {
    width: 100%;
    height: 100%;
  }
}
.nav-list {
  display: flex;
  justify-content: space-around;
  margin: 30rpx 0;

  .nav-img {
    width: 128rpx;
    height: 140rpx;
  }
}

.floor-title {
  display: flex;
  width: 100%;
  height: 60rpx;
}

.right-img-box {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
}

.floor-img-box {
  display: flex;
  padding-left: 10rpx;
}
</style>
